import 'package:dio/dio.dart';
import 'package:flutter_ebook/http/http_method.dart';
import 'package:flutter_ebook/http/print_log_interceptor.dart';
import 'package:flutter_ebook/http/response_interceptor.dart';

class DioInstance {
  // 单例模式的Dio实例类
  static DioInstance? _instance; // 私有静态变量，用于存储单例实例
  DioInstance._(); // 私有构造函数，防止外部实例化

  static DioInstance instance() {
    // 获取单例实例的静态方法
    return _instance ??= DioInstance._(); // 如果实例不存在则创建新实例
  }

  final Dio _dio = Dio(); // 创建Dio实例
  final _defaultTimeout = const Duration(seconds: 30); // 默认超时时间
  void initDio({
    // 初始化Dio配置的方法
    String? httpMethod = HttpMethod.get, // HTTP方法，默认为GET
    Duration? connectTimeout, // 连接超时时间
    Duration? receiveTimeout, // 接收超时时间
    Duration? sendTimeout, // 发送超时时间
    ResponseType? responseType = ResponseType.json, // 响应类型，默认为JSON
    String? contentType, // 内容类型
    Map<String, dynamic>? headers, // 请求头
  }) {
    _dio.options = BaseOptions(
      // 设置Dio的配置选项
      method: httpMethod,
      connectTimeout: connectTimeout ?? _defaultTimeout, // 连接超时时间，使用默认值如果未提供
      receiveTimeout: receiveTimeout ?? _defaultTimeout, // 接收超时时间，使用默认值如果未提供
      sendTimeout: sendTimeout ?? _defaultTimeout, // 发送超时时间，使用默认值如果未提供
      responseType: responseType,
      contentType: contentType,
      headers: headers,
    );
    // 返回结果拦截器
    _dio.interceptors.add(ResponseInterceptor());
    _dio.interceptors.add(PrintLogInterceptor());
  }

//get method
  Future<Response> get({
    required String path,
    Object? data,
    Map<String, dynamic>? queryParameters,
    Options? options,
    CancelToken? cancelToken,
  }) async {
    return _dio.get(
      path,
      queryParameters: queryParameters,
      data: data,
      options: options ??
          Options(
            method: HttpMethod.get,
            receiveTimeout: _defaultTimeout,
            sendTimeout: _defaultTimeout,
          ),
      cancelToken: cancelToken,
    );
  }

// getString方法，主要获取html数据
  Future<String> getString({
    required String path,
    Object? data,
    Map<String, dynamic>? queryParameters,
    Options? options,
    CancelToken? cancelToken,
  }) async {
    return (await _dio.get(
      path,
      queryParameters: queryParameters,
      data: data,
      options: options ??
          Options(
            method: HttpMethod.get,
            receiveTimeout: _defaultTimeout,
            sendTimeout: _defaultTimeout,
          ),
      cancelToken: cancelToken,
    ))
        .data
        .toString();
  }

  // post 方法获取json数据
  Future<Response> post({
    required String path,
    Object? data,
    Map<String, dynamic>? queryParameters,
    Options? options,
    CancelToken? cancelToken,
  }) async {
    return _dio.post(
      path,
      data: data,
      queryParameters: queryParameters,
      options: options ??
          Options(
            method: HttpMethod.post,
            receiveTimeout: _defaultTimeout,
            sendTimeout: _defaultTimeout,
          ),
      cancelToken: cancelToken,
    );
  }
}
